package com.zhanglinwei.openApiClient.segment;

import org.apache.commons.lang3.StringUtils;

public enum SQLKeyword implements SQLSegment {

    SELECT_ALL("*"),
    AS(" AS "),
    SELECT("SELECT"),
    FORM("FROM"),
    AND("AND"),
    OR("OR"),
    IN("IN"),
    NOT_IN("NOT IN"),
    LIKE("LIKE"),
    NOT_LIKE("NOT LIKE"),
    EQ("="),
    NE("<>"),
    GT(">"),
    GE(">="),
    LT("<"),
    LE("<="),
    DISTINCT("DISTINCT"),
    WHERE("WHERE"),
    IS_NULL("IS NULL"),
    IS_NOT_NULL("IS NOT NULL"),
    GROUP_BY("GROUP BY"),
    HAVING("HAVING"),
    ORDER_BY("ORDER BY"),
    BETWEEN("BETWEEN"),
    NOT_BETWEEN("NOT BETWEEN"),
    LIMIT("LIMIT"),
    ASC("ASC"),
    DESC("DESC"),
    ;

    private final String keyword;

    SQLKeyword(String keyword) {
        this.keyword = keyword;
    }

    public static boolean matchSegment(String input) {
        if (StringUtils.isBlank(input)) {
            return false;
        }

        for (SQLKeyword keyword : SQLKeyword.values()) {
            if (keyword.sqlSegment().equals(input)) {
                return true;
            }
        }

        return false;
    }

    public boolean isWhere() {
        return SQLKeyword.WHERE.equals(this);
    }

    public boolean isHaving() {
        return SQLKeyword.HAVING.equals(this);
    }

    public boolean isInCondition() {
        return SQLKeyword.IN.equals(this) || SQLKeyword.NOT_IN.equals(this);
    }

    public boolean isOnlyKeywordCondition() {
        return SQLKeyword.IS_NULL.equals(this) || SQLKeyword.IS_NOT_NULL.equals(this);
    }

    public boolean isBetweenCondition() {
        return SQLKeyword.BETWEEN.equals(this) || SQLKeyword.NOT_BETWEEN.equals(this);
    }

    @Override
    public String sqlSegment() {
        return this.keyword;
    }
}
